<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实验编号">
							<el-input v-model="state.ruleForm.experimentId" placeholder="请输入实验编号" clearable disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实验名称">
							<el-input v-model="state.ruleForm.experimentName" placeholder="请输入实验名称" clearable disabled />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="学号">
							<el-input v-model="state.ruleForm.studentId" placeholder="请输入学号" clearable disabled />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="学生姓名">
							<el-input v-model="state.ruleForm.studentName" placeholder="请输入学生姓名" clearable disabled />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实验原理">
							<el-input v-model="state.grade.a1" placeholder="请对学生实验原理掌握情况进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="理论知识">
							<el-input v-model="state.grade.a2" placeholder="请对学生理论知识掌握情况进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="仪器设备">
							<el-input v-model="state.grade.a3" placeholder="请对学生仪器设备知悉程度进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="操作流程">
							<el-input v-model="state.grade.a4" placeholder="请对学生操作流程掌握情况进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="数据记录">
							<el-input v-model="state.grade.c1" placeholder="请对学生数据记录情况进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="现象描述">
							<el-input v-model="state.grade.c2" placeholder="请对学生实验现象描述情况进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="数据分析">
							<el-input v-model="state.grade.c3" placeholder="请对学生数据分析能力进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="变化规律">
							<el-input v-model="state.grade.c4" placeholder="请对学生实验变化分析情况进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="考勤">
							<el-input v-model="state.grade.d1" placeholder="请对学生考勤进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实验报告">
							<el-input v-model="state.grade.d2" placeholder="请对学生实验报告进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实验总结">
							<el-input v-model="state.grade.d3" placeholder="请对学生实验总结进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="心得体会">
							<el-input v-model="state.grade.d4" placeholder="请对学生心得体会进行评分" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="gradeManagementDialog">
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { getTeacherEvaluationInfo, updateTeacherGrade } from '/@/api/grade';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const roleDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		experimentId: '',
		experimentName: '',
		studentId: '',
		studentName: '',
		class: '',
	},
	grade: {
		a1: 0,
		a2: 0,
		a3: 0,
		a4: 0,
		c1: 0,
		c2: 0,
		c3: 0,
		c4: 0,
		d1: 0,
		d2: 0,
		d3: 0,
		d4: 0,
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (row: gradeManagementType) => {
	state.ruleForm = row;
	state.dialog.title = '教师评分';
	state.dialog.submitTxt = '完 成';
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	getTeacherEvaluationInfo({ experiment_id: state.ruleForm.experimentId, student_id: state.ruleForm.studentId })
		.then((res) => {
			if (res.code === '2000') {
				const update_info = {
					evaluated_id: res.data[0].evaluated_id,
					info: {
						teacher_grade:
							state.grade.a1 * 0.12 +
							state.grade.a2 * 0.05 +
							state.grade.a3 * 0.03 +
							state.grade.a4 * 0.05 +
							state.grade.c1 * 0.12 +
							state.grade.c2 * 0.05 +
							state.grade.c3 * 0.04 +
							state.grade.c4 * 0.05 +
							state.grade.d1 * 0.12 +
							state.grade.d2 * 0.05 +
							state.grade.d3 * 0.04 +
							state.grade.d4 * 0.04,
					},
				};
				updateTeacherGrade(update_info)
					.then((result) => {
						if (result.code === '2000') {
							ElMessage.success('更新评分成功!');
							closeDialog();
							emit('refresh');
						} else {
							ElMessage.error('更新评分失败!');
						}
					})
					.catch((err) => {
						ElMessage.error('更新评分失败!' + err);
					});
			} else {
				ElMessage.error('获取评价信息失败!');
			}
		})
		.catch((err) => {
			ElMessage.error('获取评价信息失败!' + err);
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
